package com.itheima.edu.manager1.service;

import com.itheima.edu.manager1.dao.TeacherDao;
import com.itheima.edu.manager1.domain.Teacher;

public class TeachService {
    public boolean addTeacher(Teacher teacher) {
        //创建TeacherDao对象
        TeacherDao teacherDao = new TeacherDao();
        //将接受的教师对象传递给它,并且将返回值返还给teacherController
        return teacherDao.addTeacher(teacher);

    }

    public boolean isExists(String id) {
        //从TeacherDao中调用findAllTeacher方法查看所有数组
        TeacherDao teacherDao = new TeacherDao();
        Teacher[] teacher = teacherDao.findAllTeacher();//返回的是个数组
        //遍历数组看每一个对象,判断学号是否存在,标记位思想Boolean flag = false
        boolean flag = false;
        for (int i = 0; i < teacher.length; i++) {
            Teacher teacher1 = teacher[i];
            //如果teacher1不为空指针且与id对比相等则说明数组中有这个id
            if(teacher1 != null && teacher1.getId().equals(id)){
                flag = true;
                break;
            }
        }
        //返回boolean结果
        return flag;
    }

    public Teacher[] findAllTeacher() {
        TeacherDao teacherDao = new TeacherDao();
        Teacher[] teacher = teacherDao.findAllTeacher();
         /*
            2. 对库管查到的教师数据进行判断
                2.1 如果库管给的教师数组里装的全是null,我们就直接返回一个null给客服
                2.2 如果库管给的教师数组里有一个不是null,就说明系统中有学生信息,我们就把整个数组返回给客服
         */
        //先定义一个标记: boolean flag = false,默认值表示先假设数组里全是null
        boolean flag = false;
        for (int i = 0; i < teacher.length; i++) {
            Teacher teacher1 = teacher[i];
            if(teacher1 != null){
                flag = true;
                break;
            }
        }
        if(flag){
            return teacher;
        }else{
            return null; //如果这里返回null在TescherController中就会显示查无信息.
        }
    }
}

